﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameworkManapro.BusinessModel;
using FrameworkManapro.Utilities;
using FrameworkManapro.WebUI;
using GMAC.ReportingSystem.Business.Entities;

namespace GMAC.ReportingSystem.Business.Components
{
    public class AccountDebitBalanceComponent : IBusinessComponent<Entities.AccountDebitBalance>
    {
        private DataAccess.AccountDebitBalanceDataAccess dac;

        #region Constructors

        public AccountDebitBalanceComponent()
        {
            dac = new DataAccess.AccountDebitBalanceDataAccess();
        }

        #endregion

        ////////////////////////////////////////////////////////////////////

        #region IBusinessComponent<AccountDebitBalanceDataAccess> Members

        public int Create(Entities.AccountDebitBalance entityToCreate)
        {
            if (Validate(entityToCreate))
            {
                var result = dac.Insert(entityToCreate);

                return result;
            }
            return 0;
        }

        //------------------------------------------------------

        public int Delete(Entities.AccountDebitBalance entityToDelete)
        {
            if (string.IsNullOrEmpty(entityToDelete.AccountNumber))
            {
                throw new NullReferenceException("AccountNumber cannot be null");
            }

            return dac.Delete(entityToDelete);
        }

        //------------------------------------------------------

        public int Edit(Entities.AccountDebitBalance entityToEdit)
        {
            if (string.IsNullOrEmpty(entityToEdit.AccountNumber))
            {
                throw new NullReferenceException("AccountNumber cannot be null");
            }

            return Validate(entityToEdit) ? dac.Update(entityToEdit) : 0;
        }

        //------------------------------------------------------

        public Entities.AccountDebitBalance GetDetails(object entityId)
        {
            return dac.Get(entityId);
        }

        //------------------------------------------------------

        protected bool Validate(Entities.AccountDebitBalance accountDebitBalance)
        {
            if (accountDebitBalance == null)
            {
                throw new ArgumentNullException("AccountDebitBalance", "The argument cannot be null.");
            }
            if (string.IsNullOrEmpty(accountDebitBalance.AccountNumber.ToString()))
            {
                throw new ArgumentNullException("AccountNumber", "The argument cannot be null.");
            }
            if (string.IsNullOrEmpty(accountDebitBalance.AccountType.ToString()))
            {
                throw new ArgumentNullException("AccountType", "The argument cannot be null.");
            }
            if (string.IsNullOrEmpty(accountDebitBalance.AccountTypeID.ToString()))
            {
                throw new ArgumentNullException("AccountTypeID", "The argument cannot be null.");
            }
            if (string.IsNullOrEmpty(accountDebitBalance.DebitBankNumber))
            {
                throw new ArgumentNullException("DebitBank.DebitBankNumber", "The argument cannot be null.");
            }
            if (string.IsNullOrEmpty(accountDebitBalance.InsuranceCompany.Id.ToString()))
            {
                throw new ArgumentNullException("InsuranceCompany.Id", "The argument cannot be null.");
            }
            if (string.IsNullOrEmpty(accountDebitBalance.IssueDate.ToString()))
            {
                throw new ArgumentNullException("IssueDate", "The argument cannot be null.");
            }
            if (string.IsNullOrEmpty(accountDebitBalance.PaymentType.ToString()))
            {
                throw new ArgumentNullException("PaymentType", "The argument cannot be null.");
            }
            if (string.IsNullOrEmpty(accountDebitBalance.StatusLAORetail.ToString()))
            {
                throw new ArgumentNullException("StatusLAORetail", "The argument cannot be null.");
            }
            if (string.IsNullOrEmpty(accountDebitBalance.StatusSalvageTransferReason.ToString()))
            {
                throw new ArgumentNullException("StatusSalvageTransferReason", "The argument cannot be null.");
            }
            if (string.IsNullOrEmpty(accountDebitBalance.PendingAmount.ToString()))
            {
                throw new ArgumentNullException("PendingAmount", "The argument cannot be null.");
            }


            return true;
        }

        //------------------------------------------------------

        #endregion

        ////////////////////////////////////////////////////////////////////

        #region Public Methods

        public Entities.AccountDebitBalanceCollection GetList()
        {
            return dac.GetList();
        }

        //------------------------------------------------------

        public Entities.AccountDebitBalanceCollection Search(string accountNumber, GeneralEnumTypes.TypeAccountsWithDebitBalances? type, int? insuranceCompanyId, DateTime? issueDate, int? emissionDays, GeneralEnumTypes.EmissionTime? emissionTime, PagingDetails paging)
        {
            if (accountNumber == null)
            {
                accountNumber = string.Empty;
            }

            return dac.Search(accountNumber, type, insuranceCompanyId, issueDate, emissionDays, emissionTime, paging);
        }

        #endregion
    }
}
